<template>
  <div class="search-history">
    <van-cell title="搜索历史">
      <van-icon
        slot="default"
        name="delete-o"
        @click="clearFn"
      />
    </van-cell>
    <van-cell
      v-for="(item,index) in searchHistorys"
      :key="index"
      :title="item"
      @click="$emit('search',item)"
    >
      <van-icon
        slot="default"
        name="cross"
        @click.stop="deleteFn(index)"
      />
    </van-cell>
  </div>
</template>

<script>
export default {
  name: 'SearchHistory',
  props: {
    searchHistorys: {
      type: Array,
      required: true
    }
  },
  methods: {
    clearFn () {
      this.$dialog.confirm({
        title: '确认删除全部历史记录？'
      })
        .then(() => {
          // on confirm
          this.$emit('clearHistory')
        })
        .catch(() => {
          // on cancel
        })
    },
    deleteFn (index) {
      this.searchHistorys.splice(index, 1)
    }
  }
}
</script>

<style>
</style>
